package cn.tj.wenda.cloud.config;

import cn.tj.wenda.cloud.util.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.context.request.RequestContextListener;

import cn.tj.wenda.cloud.interceptor.JwtFeignInterceptor;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author Wenda
 */
@ComponentScan(basePackages = "cn.tj.wenda.cloud")
public class CommonConfig {

	public final static String ROOT_PACKAGE = "cn.tj.wenda.cloud";
	
	@Bean(name="entityUtil")
	public EntityUtil getEntityUtil() {
		return new EntityUtil();
	}
	
	@Bean(name="primaryKey")
	public PrimaryKey getPrimaryKey() {
		return new PrimaryKey();
	}
	
	@Bean(name="dicMessage")
	public DicMessage getDicMessage() {
		return new DicMessage();
	}
	
	@Bean(name="appConfig")
	public AppConfig getAppConfig() {
		return new AppConfig();
	}

	@Bean(name="objectMapper ")
	public ObjectMapper getObjectMapper(){
		return new ObjectMapper();
	}

	@Bean
	public RequestContextListener requestContextListener() {
		return new RequestContextListener();
	}

	@Bean
	public JwtFeignInterceptor basicAuthRequestInterceptor() {
		return new JwtFeignInterceptor();
	}

	@Bean
	public CorsFilter corsFilter() {
		CorsConfiguration corsConfiguration = new CorsConfiguration();
		corsConfiguration.addAllowedOrigin("*");
		corsConfiguration.addAllowedHeader("*");
		corsConfiguration.addAllowedMethod("*");

		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		// 配置所有请求
		source.registerCorsConfiguration("/**", corsConfiguration);
		return new CorsFilter(source);
	}

}
